let id = location.href.split('?')[1].split('=')[1]
let gamename = document.querySelector('.gamename'),
    cover = document.querySelector('.cover'),
    publishdate = document.querySelector('.publishdate'),
    description = document.querySelector('textarea'),
    system_radios = document.querySelectorAll('.system'),
    copyright_select = document.querySelector('.copyright'),
    type_select = document.querySelector('.type'),
    cover_img = document.querySelector('.cover_img')

const getcopyrights = async() => {
    try {
        let response = await ajax('getcopyrights?pagenum=1&pagesize=20')
        let copys = response.data
        copyright_select = document.querySelector('.copyright')
        for (const copy of copys) {
            let option = document.createElement('option')
            option.innerHTML = copy.copyright_name
            option.value = copy.id
            copyright_select.add(option)
        }
    } catch (error) {
        alert(error.meta.msg)
    }
}

const getgametype = async() => {
    try {
        let response = await ajax('getgametypes?pagenum=1&pagesize=20')
        let types = response.data
        type_select = document.querySelector('.type')
        for (const type of types) {
            let option = document.createElement('option')
            option.innerHTML = type.typename
            option.value = type.id
            type_select.add(option)
        }
    } catch (error) {
        alert(error.meta.msg)
    }
}

const perviewimg = () => {
    let cover = _this.value
    if (cover) {
        document.querySelector('.cover_img').src = cover
    }
}

const gameregtime = time => {
    let time1 = time.split('T')[0]
    return `${time1}`
}
const getupdate_gameid = async() => {
    try {
        const response = await ajax(`get_onegames?id=${id}`)
        let game = response.data[0]
        gamename.value = game.gamename
        cover.value = game.cover

        publishdate.value = gameregtime(game.publishdate)
        description.value = game.description
            // 赋值下拉框

        type_select.value = game.type
        copyright_select.value = game.copyright


        for (const radio of system_radios) {
            if (radio.value == game.system) radio.checked = true
        }
        if (cover.value) {
            cover_img.src = game.cover
        }
    } catch (error) {
        alert(error.meta.msg)
    }
}

const update_game = async() => {
    let gamename_x = gamename.value,
        cover_x = cover.value,
        publishdate_x = publishdate.value,
        description_x = description.value,
        system,
        copyright = copyright_select.value,
        type = type_select.value

    if (type_select.selectedIndex == 0) alert('请选择游戏类型')
    if (copyright_select.selectedIndex == 0) alert('请选择游戏公司')

    for (const radio of system_radios) {
        if (radio.checked) {
            system = radio.value
        }
    }
    let obj = {
        id,
        gamename: gamename_x,
        cover: cover_x,
        publishdate: publishdate_x,
        description: description_x,
        system,
        copyright,
        type
    }
    const response = await ajax('updategame', 'PUT', obj)
    alert(response.meta.msg)
    location.href = './games.html'
}

window.onload = function() {
    getcopyrights()
    getgametype()
    getupdate_gameid()
}